package com.elinkway.infinitemovies.async;

import com.elinkway.infinitemovies.bean.HtmlDataBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.HtmlParser;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.RequestSniffDataTaskPoolManager;
import com.elinkway.infinitemovies.utils.Utils;
import com.lvideo.http.bean.LVideoDataHull;

/**
 * 请求截流 apiContent 线程类
 * <p/>
 * Created by wangyemin on 2015/12/11.
 */
public class RequestSniffApiContentTask implements Runnable {

    private int mtaskId;
    private String mUrl;
    private String mReferer;
    private String mUserAgent;

    public RequestSniffApiContentTask(int mtaskId, String mUrl, String mReferer, String mUserAgent) {
        this.mtaskId = mtaskId;
        this.mUrl = mUrl;
        this.mReferer = mReferer;
        this.mUserAgent = mUserAgent;
    }

    @Override
    public void run() {
        LogUtils.e(Utils.TAG, "!!!!!!!!!!!!" + Thread.currentThread().getName() + " Start");
        LVideoDataHull obj = MoviesHttpApi.requestHtmlData(new HtmlParser(), mUrl, mReferer, mUserAgent);
        if (obj.getDataType() == LVideoDataHull.DataType.DATA_IS_INTEGRITY) {
            if (null != mListener)
                mListener.onRequestSuccess(obj.getUpdataId(), (HtmlDataBean) obj.getDataEntity());
        } else {
            if (null != mListener)
                mListener.onRequestFailed();
        }
        LogUtils.e(Utils.TAG, "!!!!!!!!!!!!" + Thread.currentThread().getName() + " End");
    }

    RequestResultListener<HtmlDataBean> mListener = new RequestResultListener<HtmlDataBean>() {
        @Override
        public boolean onRequestFailed() {
            return false;
        }

        @Override
        public void onRequestSuccess(int updateId, HtmlDataBean result) {
            if (null != result) {
                String apiContent = result.getHtmlData();
                LogUtils.e(Utils.TAG, "!!!!!!!!!!!RequestSniffApiContentTask cur id is " + mtaskId + " request apicontent ok ！ " + "\n return apiContent is " + apiContent);
                RequestSniffDataTaskPoolManager.get().setResultList(apiContent, mtaskId);
                LogUtils.e(Utils.TAG, " pool resultList size " + RequestSniffDataTaskPoolManager.get().getResultList().size());
            } else
                LogUtils.e(Utils.TAG, "!!!!!!!!!!!!!requset apiContent result is null!!!!!!!!!!!!");
        }

        @Override
        public void onPreRequest() {
        }
    };
}